/* Copyright 2009 Palm, Inc.  All rights reserved. */

var DialingshortcutAssistant = Class.create({
    initialize: function(mainSceneAssistant){
		this.mainSceneAssistant = mainSceneAssistant;		
    },
    
    setup: function(){
		this.digitList = [{label:$L('4 digits'), value:"4DigitNumber"},
							{label:$L('5 digits'), value:"5DigitNumber"},
		 					{label:$L('6 digits'), value:"6DigitNumber"},
							{label:$L('7 digits'), value:"7DigitNumber"}
						 ];
		this.digitListModel = {
				currentSelection: '4DigitNumber',
				choices: this.digitList
		};
		
		this.dsAttributes = {
		hintText: $L('Enter number'),
		modelProperty: 'number',
		modifierState: Mojo.Widget.numLock,
		focusMode: Mojo.Widget.focusInsertMode,
		disabledProperty: 'p',
		changeOnKeyPress: true,
		charsAllow: Mojo.Widget.numericValidation,
		maxLength: 50
		};
		this.shortcutNumbermodel = {
		        p: false,
		        'number': ''			
		};
		this.controller.setupWidget('shortcut_prefix', this.dsAttributes, this.shortcutNumbermodel);
		this.controller.get('shortcut_prefix').observe(Mojo.Event.propertyChange, this.propertyChanged.bind(this));
		
		this.controller.setupWidget('listDigits_selector', {modelProperty:'currentSelection'}, this.digitListModel);			
		this.controller.get('listDigits_selector').addEventListener(Mojo.Event.propertyChange, this.handleDigitSelection.bindAsEventListener(this));
		
		this.doneButtonModel = {buttonLabel: $L("Done"), disabled:true};
		this.controller.setupWidget('done_button', {}, this.doneButtonModel);
        this.controller.get('done_button').addEventListener(Mojo.Event.tap, this.handleDone.bindAsEventListener(this));
				
		// App Menu - Remove default items and add only Edit & Help items.
		var appMenuModel = {
	        visible: true,
            items: [Mojo.Menu.editItem, {label:$L('Help'), command:Mojo.Menu.helpCmd}]
        };
		this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems:true}, appMenuModel);
    },
	
	handleCommand: function(event) {
		if (event.type == Mojo.Event.back) {
			if(this.doneButtonModel.disabled === false){
				this.handleDone();
				event.stopPropagation();
				Event.stop(event);
			}
		} else if (event.type == Mojo.Event.command) {
			if (event.command == Mojo.Menu.helpCmd) {
				var r = new Mojo.Service.Request('palm://com.palm.applicationManager', {
					method: 'open',
					parameters: {'id': 'com.palm.app.help',params: {'target': 'http://help.palm.com/phone/phone_prefs/index.html'}}});
			}
		}
	},	
	
	handleDigitSelection: function(event) {
		if(!event.value) {
			return;		
		}
	},
	
	propertyChanged: function(event) {
		
		if(this.shortcutNumbermodel.number.length > 0) {
			this.doneButtonModel.disabled = false;
		} else {
			this.doneButtonModel.disabled = true;
		}
		this.controller.modelChanged(this.doneButtonModel);
	},
 	
	handleDone: function() {
				
		var obj = {
			text: this.digitListModel.currentSelection,
			value: this.shortcutNumbermodel.number
		};
		
		SystemService.setSystemPreferencesService(this.digitListModel.currentSelection, this.shortcutNumbermodel.number,null);
		this.mainSceneAssistant.dialingShortCutAdded(obj);
		Mojo.Controller.stageController.popScene();
	}
	
});

